﻿using System;
using System.Windows;
using System.Net;
using System.Windows.Media.Imaging;
using System.IO;
using Amercom.Silverlight.Lib;

namespace Amercom.Silverlight.Helpers.PanoramaCube
{
    public class Face
    {
        private int _vertexA;
        private int _vertexB;
        private int _vertexC;
        private int _vertexD;

        public int VertexA { get { return (_vertexA); } set { _vertexA = value; } }
        public int VertexB { get { return (_vertexB); } set { _vertexB = value; } }
        public int VertexC { get { return (_vertexC); } set { _vertexC = value; } }
        public int VertexD { get { return (_vertexD); } set { _vertexD = value; } }

        private Uri _textureUri;

        public Uri TextureUri { get { return _textureUri; } set { _textureUri = value; } }

        public BitmapImage _textureBitmap {get; set;}
        
        private DistortImage _distort;
        public DistortImage distort {get{return _distort;} set {_distort=value;}}


        public Boolean IsTextureLoaded = false;
        public Stream stream;
        public BitmapImage TextureBitmap = new BitmapImage();
        public String Name;
        //      public Control renderControl;
        public Face(String Name, int VertexA, int VertexB, int VertexC, int VertexD, Uri TextureUri)
        {
            this.Name = Name;
            this.VertexA = VertexA;
            this.VertexB = VertexB;
            this.VertexC = VertexC;
            this.VertexD = VertexD;

            this._textureUri = TextureUri;
            this.IsTextureLoaded = true;


            TextureBitmap = new BitmapImage(TextureUri);
            this.IsTextureLoaded = true;
/*
            //// load image into brush
            WebClient webClient = new WebClient();
            
            webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
            webClient.OpenReadAsync(this.TextureUri);


            //BitmapImage bitmap = new BitmapImage(this.TextureUri, UriKind.Absolute);
            //bitmap.ImageFailed += new ExceptionRoutedEventHandler(bitmap_ImageFailed);
            //bitmap.DownloadProgress += new DownloadProgressEventHandler(bitmap_DownloadProgress);
            //content.ImageContent.Source = bitmap;
            */
        }

        void bitmap_DownloadProgress(object sender, DownloadProgressEventArgs e)
        {
            throw new NotImplementedException();
        }

        void bitmap_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            throw new NotImplementedException();
        }
       
        void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                DebugLogger.debugWrite("*************************************\naTexture loaded : " + e.Result.ToString());
                TextureBitmap = new BitmapImage();
                TextureBitmap.SetSource(e.Result);
//                this.stream = e.Result;
                this.IsTextureLoaded = true;
            }
            else
            {
                DebugLogger.debugWrite("*************************************\nbTexture Error : " + e.Error.ToString());                
            }

        }
    }
}
